package com.shushan.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.shushan.home.bean.StarsDataBean;
import com.shushan.shushanbao.R;
import com.shushan.util.ImageUtils;

import java.util.List;

/**
 * @ClassName: StarMallRecyclerAdapter
 * @Desciption: //主页星星商城列表recyclerview适配器
 * @author: zhangshihao
 * @date: 2018-05-11
 */
public class StarMallRecyclerAdapter extends RecyclerView.Adapter<StarMallRecyclerAdapter.ViewHolder>{

    private Context context;
    private List<StarsDataBean> starsDataBeanList;
    private OnItemClickListener listener;

    public StarMallRecyclerAdapter(Context context,List<StarsDataBean> starsDataBeanList){
        this.context = context;
        this.starsDataBeanList = starsDataBeanList;
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_star_good,null,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        StarsDataBean bean = starsDataBeanList.get(position);
        ImageUtils.loadImageView(context,bean.goods_cover,holder.ivGoodPic);
        holder.tvGoodName.setText(bean.goods_name);
        holder.tvGoodPrice.setText(bean.price+"");
        holder.tvGoodStarPrice.setText(bean.stars+"");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return starsDataBeanList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView ivGoodPic;
        private TextView tvGoodName;
        private TextView tvGoodPrice;
        private TextView tvGoodStarPrice;

        public ViewHolder(View itemView) {
            super(itemView);
            ivGoodPic = itemView.findViewById(R.id.iv_good_pic);
            tvGoodName = itemView.findViewById(R.id.tv_good_name);
            tvGoodPrice = itemView.findViewById(R.id.tv_good_price);
            tvGoodStarPrice = itemView.findViewById(R.id.tv_good_star_price);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

}
